package com.praveen.twu.managers;

import com.praveen.twu.Constants;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by praveen on 20/6/14.
 */
public class MenuManager {
    static ArrayList<String> m = new ArrayList<String>();
    static Scanner in = new Scanner(System.in);

    public static void populate() {
        m.clear();
        m.add("List Books");
        m.add("List Movies");
        m.add("Checkout Book");
        m.add("Return Book");
        m.add("Checkout Movie");
        m.add("Return Movie");
        m.add("Login");
        m.add("Quit");
    }

    private static String printable() {
        String str = "\nPlease select an option\n";
        for (int i = 0; i < m.size(); i++) {
            str += ("\n" + (i + 1) + ". " + m.get(i));
        }
        return str;
    }

    private static void printDotLine() {
        System.out.println("------------------------------------");
    }

    private static void printMenu() {
        printDotLine();
        System.out.println(printable());
        printDotLine();
    }

    public static int getItemCount() {
        return m.size();
    }

    public static void displayMenu() {
        while (true) {
            printMenu();
            try {
                if (!MenuManager.handleSelect(Integer.parseInt(in.nextLine())))
                    break;
            } catch (Exception e) {
                System.out.println(Constants.OPTION_INVALID);
                continue;
            }
        }
    }

    private static boolean handleSelect(int input) {
        if (input == MenuManager.getItemCount()) {
            return false;
            //User quits.
        }
        boolean login = LoginManager.isLoggedIn();
        switch (input) {
            case 1:
                System.out.println(BookManager.getAvailableBookList());
                break;
            case 2:
                System.out.println(MovieManager.getAvailableMovieList());
                break;
            case 3:
                checkoutBook();
                break;
            case 4:
                returnBook();
                break;
            case 5:
                checkoutMovie();
                break;
            case 6:
                returnMovie();
                break;
            case 7:
                if (!LoginManager.isLoggedIn())
                    login();
                else
                    System.out.println(LoginManager.getAccount());
                break;
            default:
                System.out.println(Constants.OPTION_INVALID);
                break;
        }
        return true;
    }

    private static void login() {
        System.out.println(Constants.ENTER_LIBRARY_NUMBER);
        String username = in.nextLine();
        System.out.println(Constants.ENTER_PASSWORD);
        String password = in.nextLine();
        boolean login = LoginManager.login(username, password);
        System.out.println(login ? Constants.GOOD_LOGIN : Constants.BAD_LOGIN);
        if (login)
            m.set(6, "View Account Details");
    }

    private static void checkoutBook() {
        System.out.println(Constants.ENTER_ISBN);
        System.out.println(BookManager.checkout(in.nextLine()) != null ? Constants.OK_CHECKOUT : LoginManager.isLoggedIn() ? Constants.BAD_CHECKOUT : Constants.NOT_LOGGED_IN);
    }

    private static void returnBook() {
        System.out.println(Constants.ENTER_ISBN);
        System.out.println(BookManager.returnBook(in.nextLine()) != null ? Constants.OK_RETURN : LoginManager.isLoggedIn() ? Constants.BAD_RETURN : Constants.NOT_LOGGED_IN);
    }

    private static void checkoutMovie() {
        System.out.println(Constants.ENTER_NAME);
        System.out.println(MovieManager.checkout(in.nextLine()) != null ? Constants.MOVIE_OK_CHECKOUT : LoginManager.isLoggedIn() ? Constants.MOVIE_BAD_CHECKOUT : Constants.NOT_LOGGED_IN);
    }

    private static void returnMovie() {
        System.out.println(Constants.ENTER_NAME);
        System.out.println(MovieManager.returnMovie(in.nextLine()) != null ? Constants.MOVIE_OK_RETURN : LoginManager.isLoggedIn() ? Constants.MOVIE_BAD_RETURN : Constants.NOT_LOGGED_IN);
    }
}